﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class BM2 : BasePage
{

    string emId = string.Empty;
    string emYear = string.Empty;
    string emNum = string.Empty;
    string applyId = string.Empty;
    string emRankName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Count > 0)
        {
            emYear = Request.Params["EmYear"].Trim();
            emNum = Request.Params["EmNum"].Trim();
            emId = Request.Params["EmID"].Trim();
        }
        else
        {
            Response.Redirect(BSUtility.mainUrl + "default.aspx");
        }
        NoAccess.JumpToDefaultPageNoAccessTime(this, emId);
        
        //用户信息是否确认,通过订单信息来确认用户是否确认
        //if (!checkOrderInfo(studIdcard, emId))
        //{
        //    DataTable dt = Exam.getOrderInfoByMsg(studIdcard, emId);
        //    DataRow dr = dt.Rows[0];
        //    string orderId = dr["OrderID"].ToString();
        //    Response.Redirect("BM4.aspx?OrderID=" + orderId);
        //}
        DataTable dt_apply = Exam.getBMInfoByStudIdCard(IDCard, emId,emYear,emNum);
        if (dt_apply.Rows.Count > 0)
        {
            DataRow dr_apply = dt_apply.Rows[0];
            string applyState = dr_apply["ApplyState"].ToString();
            string orderId = dr_apply["OrderID"].ToString();
            if (applyState.Trim() == "1")
            {
                Response.Redirect(BSUtility.mainUrl + "BM/BM4.aspx?OrderID=" + orderId);
            }
            else if (applyState.Trim() == "2")
            {
                Response.Redirect(BSUtility.mainUrl + "BM/BM5.aspx?OrderID=" + orderId);
            }
        }


        if (!Page.IsPostBack)
        {
            DataTable dt = Exam.getBMInfoByStudIdCard(IDCard,emId,emYear,emNum);
            if (dt.Rows.Count >0)
            {
                DataRow dr = dt.Rows[0];
                BMIDText.Text = dr["ApplyID"].ToString();
                NameText.Text = dr["StudName"].ToString();
                SFZHText.Text = dr["StudIdcard"].ToString();
                SexText.Text = dr["StudSex"].ToString().Trim();
                SIDText.Text = dr["StudID"].ToString();
                ClassText.Text = dr["StudClassName"].ToString();
                DepText.Text = dr["StudDeptName"].ToString();
                ExamNameText.Text = dr["EmAllName"].ToString();
                LevelName_Text.Text = dr["EmRankName"].ToString();
                CostText.Text = dr["EmCost"].ToString();
                TelPhoneText.Text = dr["StudTel"].ToString();
            }
        }
    }
    protected void SInfoBtn_Click(object sender, EventArgs e)
    {
        int orderId;
        DataTable dt = Exam.getBMInfoByStudIdCard(IDCard, emId,emYear,emNum);
        if (dt.Rows.Count > 0)
        {
            DataRow dr = dt.Rows[0];
            emRankName = dr["EmRankName"].ToString();
            applyId = dr["ApplyID"].ToString();
            emId = dr["EmID"].ToString();
        }

        
        orderId = Exam.CreateOrderInfo(IDCard, emRankName, emYear, emNum, emId);
        //向报名表中添加订单号,且把报名状态更新到已经确认
        if (Exam.updateOrderIDInApplyInfo(applyId, orderId.ToString()) && Exam.updateApplyState(applyId,"1"))
        {
            //更新订单状态为未支付
            if (Exam.Update_OrderState(orderId.ToString(), "0"))
            {
                Response.Redirect("BM4.aspx?OrderID=" + orderId, true);
            }
        }   
        
                 
    }

    protected void SInfoDelete_Click(object sender, EventArgs e)
    {
        if (Exam.BMRestart(IDCard, emId, emYear, emNum))
        {
            Response.Redirect(BSUtility.mainUrl + "BM/ExamSelect.aspx");
        }
        else
        {

        }
        
    }



}